package com.baizhi.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class CustomerRealm extends AuthorizingRealm {
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String primaryPrincipal = (String) principalCollection.getPrimaryPrincipal();
    System.out.println("身份信息："+primaryPrincipal);
    //根据身份信息 用户名 获取当前用户的角色信息以及权限信息 xiaomiao admin user
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
      simpleAuthorizationInfo.addRole("admin");
      simpleAuthorizationInfo.addRole("user");
      simpleAuthorizationInfo.addStringPermission("user:*:01");
      simpleAuthorizationInfo.addStringPermission("product:create");
        return simpleAuthorizationInfo;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String  principal = (String) token.getPrincipal();
    System.out.println(principal);
    if("xiaomiao".equals(principal)){
        return new SimpleAuthenticationInfo("xiaomiao","123", this.getName());
    }

        return null;
    }
}
